<!DOCTYPE html>
<html>
<head>
	<!-- Global site tag (gtag.js) - Google Analytics -->
	<script async src="https://www.googletagmanager.com/gtag/js?id='UA-133422980-2"></script>
	<script>
	  window.dataLayer = window.dataLayer || [];
	  function gtag(){dataLayer.push(arguments);}
	  gtag('js', new Date());

	  gtag('config', 'UA-133422980-2');
	</script>

	<meta charset="utf-8">
	<meta http-equiv="x-ua-compatible" content="ie=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1">

	<title>
		gem5: Configuring a simple Ruby system 
	</title>

	<!-- SITE FAVICON -->
	<link rel="shortcut icon" type="image/gif" href="/assets/img/gem5ColorVert.gif"/>

	<link rel="canonical" href="http://localhost:4000/documentation/learning_gem5/part3/configuration/">
	<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,300,700,800,600' rel='stylesheet' type='text/css'>
	<link href='https://fonts.googleapis.com/css?family=Muli:400,300' rel='stylesheet' type='text/css'>

	<!-- FAVICON -->
	<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">

	<!-- BOOTSTRAP -->
	<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

	<!-- CUSTOM CSS -->
	<link rel="stylesheet" href="/css/main.css">
</head>


<body>
	<nav class="navbar navbar-expand-md navbar-light bg-light">
  <a class="navbar-brand" href="/">
		<img src="/assets/img/gem5ColorLong.gif" alt="gem5" height=55px>
	</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse" id="navbarNavDropdown">
    <!-- LIST FOR NAVBAR -->
    <ul class="navbar-nav ml-auto">
      <!-- HOME -->
      <li class="nav-item ">
        <a class="nav-link" href="/">Home</a>
      </li>

      <!-- ABOUT -->
			<li class="nav-item dropdown ">
				<a class="nav-link dropdown-toggle" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
					About
				</a>
				<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
          <a class="dropdown-item" href="/about">About gem5</a>
          <a class="dropdown-item" href="/publications">Publications</a>
          <a class="dropdown-item" href="/governance">Governance</a>
				</div>
			</li>

      <!-- DOCUMENTATION -->
			<li class="nav-item dropdown active">
				<a class="nav-link dropdown-toggle" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
					Documentation
				</a>
				<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
					<!-- Pull navigation from _data/documentation.yml -->
					
            <a class="dropdown-item" href="/documentation">gem5 documentation</a>
					
            <a class="dropdown-item" href="/documentation/learning_gem5/introduction">Learning gem5</a>
					
            <a class="dropdown-item" href="http://doxygen.gem5.org/release/current/index.html">gem5 Doxygen</a>
					
            <a class="dropdown-item" href="/documentation/reporting_problems">Reporting Problems</a>
					
				</div>
			</li>

      <!-- EVENTS -->
			<li class="nav-item dropdown ">
        <a class="nav-link" href="/events/">Events</a>
			</li>

      <!-- CONTRIBUTING -->
      <li class="nav-item ">
        <a class="nav-link" href="/contributing">Contributing</a>
      </li>

      <!-- BLOG -->
      <li class="nav-item ">
        <a class="nav-link" href="/blog">Blog</a>
      </li>

      <!-- SEARCH -->
			<li class="nav-item ">
        <a class="nav-link" href="/search">Search</a>
      </li>
    </ul>
  </div>
</nav>

	<main>
		<div class="sidenav-top">
  <div class="sidenav-brand bg-light">
    <a href="/"><img src="/assets/img/gem5ColorLong.gif" height="55px"></a>
  </div>
  <div class="search">
    <form action="/search" method="get">
      <!-- <label for="search-box"><i class="fa fa-search"></i></label> -->
      <input type="text" name="query">
      <button type="submit" name="submit"><i class="fa fa-search"></i></button>
    </form>
  </div>
</div>
<div class="sidenav">
  <!-- Pull navigation from _data/documentation.yml -->
  
    
  
    
    
      <a class="item" href="/documentation/learning_gem5/introduction" role="button" aria-expanded="false" aria-controls="collapseExample">
        Introduction
      </a>
      <div class="collapse " id="introduction">
        
      </div>
    
      <a class="item" data-toggle="collapse" href="#part1" role="button" aria-expanded="false" aria-controls="collapseExample">
        Getting Started
      </a>
      <div class="collapse " id="part1">
        
          <a class="subitem " href="/documentation/learning_gem5/part1/building">Building gem5</a>
        
          <a class="subitem " href="/documentation/learning_gem5/part1/simple_config">Creating a simple configuration script</a>
        
          <a class="subitem " href="/documentation/learning_gem5/part1/cache_config">Adding cache to configuration script</a>
        
          <a class="subitem " href="/documentation/learning_gem5/part1/gem5_stats">Understanding gem5 statistics and output</a>
        
          <a class="subitem " href="/documentation/learning_gem5/part1/example_configs">Using the default configuration scripts</a>
        
      </div>
    
      <a class="item" data-toggle="collapse" href="#part2" role="button" aria-expanded="false" aria-controls="collapseExample">
        Modifying/Extending
      </a>
      <div class="collapse " id="part2">
        
          <a class="subitem " href="/documentation/learning_gem5/part2/environment">Setting up your development environment</a>
        
          <a class="subitem " href="/documentation/learning_gem5/part2/helloobject">Creating a very simple SimObject</a>
        
          <a class="subitem " href="/documentation/learning_gem5/part2/debugging">Debugging gem5</a>
        
          <a class="subitem " href="/documentation/learning_gem5/part2/events">Event-driven programming</a>
        
          <a class="subitem " href="/documentation/learning_gem5/part2/parameters">Adding parameters to SimObjects and more events</a>
        
          <a class="subitem " href="/documentation/learning_gem5/part2/memoryobject">Creating SimObjects in the memory system</a>
        
          <a class="subitem " href="/documentation/learning_gem5/part2/simplecache">Creating a simple cache object</a>
        
      </div>
    
      <a class="item" data-toggle="collapse" href="#part3" role="button" aria-expanded="false" aria-controls="collapseExample">
        Modeling Cache Coherence with Ruby
      </a>
      <div class="collapse show" id="part3">
        
          <a class="subitem " href="/documentation/learning_gem5/part3/MSIintro">Introduction to Ruby</a>
        
          <a class="subitem " href="/documentation/learning_gem5/part3/cache-intro">MSI example cache protocol</a>
        
          <a class="subitem " href="/documentation/learning_gem5/part3/cache-declarations">Declaring a state machine</a>
        
          <a class="subitem " href="/documentation/learning_gem5/part3/cache-in-ports">In port code blocks</a>
        
          <a class="subitem " href="/documentation/learning_gem5/part3/cache-actions">Action code blocks</a>
        
          <a class="subitem " href="/documentation/learning_gem5/part3/cache-transitions">Transition code blocks</a>
        
          <a class="subitem " href="/documentation/learning_gem5/part3/directory">MSI Directory implementation</a>
        
          <a class="subitem " href="/documentation/learning_gem5/part3/MSIbuilding">Compiling a SLICC protocol</a>
        
          <a class="subitem active" href="/documentation/learning_gem5/part3/configuration">Configuring a simple Ruby system</a>
        
          <a class="subitem " href="/documentation/learning_gem5/part3/running">Running the simple Ruby system</a>
        
          <a class="subitem " href="/documentation/learning_gem5/part3/MSIdebugging">Debugging SLICC Protocols</a>
        
          <a class="subitem " href="/documentation/learning_gem5/part3/simple-MI_example">Configuring for a standard protocol</a>
        
      </div>
    
      <a class="item" href="/documentation/learning_gem5/gem5_101/" role="button" aria-expanded="false" aria-controls="collapseExample">
        gem5 101
      </a>
      <div class="collapse " id="gem5_101">
        
      </div>
    
    
  
    
  
    
  
</div>


<div class="container" id="doc-container">
  <div class="edit"><a href="https://gem5.googlesource.com/public/gem5-website/+/refs/heads/master/README.md">Edit this page</a></div>
  <b>authors:</b> Jason Lowe-Power<br>
  

  <br>
  <h1 id="configuring-a-simple-ruby-system">Configuring a simple Ruby system</h1>

<p>First, create a new configuration directory in <code class="highlighter-rouge">configs/</code>. Just like all
gem5 configuration files, we will have a configuration run script. For
the run script, we can start with <code class="highlighter-rouge">simple.py</code> from
simple-config-chapter. Copy this file to <code class="highlighter-rouge">simple_ruby.py</code> in your new
directory.</p>

<p>We will make a couple of small changes to this file to use Ruby instead
of directly connecting the CPU to the memory controllers.</p>

<p>First, so we can test our <em>coherence</em> protocol, let’s use two CPUs.</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">system</span><span class="o">.</span><span class="n">cpu</span> <span class="o">=</span> <span class="p">[</span><span class="n">TimingSimpleCPU</span><span class="p">(),</span> <span class="n">TimingSimpleCPU</span><span class="p">()]</span>
</code></pre></div></div>

<p>Next, after the memory controllers have been instantiated, we are going
to create the cache system and set up all of the caches. Add the
following lines <em>after the CPU interrupts have been created, but before
instantiating the system</em>.</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">system</span><span class="o">.</span><span class="n">caches</span> <span class="o">=</span> <span class="n">MyCacheSystem</span><span class="p">()</span>
<span class="n">system</span><span class="o">.</span><span class="n">caches</span><span class="o">.</span><span class="n">setup</span><span class="p">(</span><span class="n">system</span><span class="p">,</span> <span class="n">system</span><span class="o">.</span><span class="n">cpu</span><span class="p">,</span> <span class="p">[</span><span class="n">system</span><span class="o">.</span><span class="n">mem_ctrl</span><span class="p">])</span>
</code></pre></div></div>

<p>Like the classic cache example in cache-config-chapter, we are going to
create a second file that contains the cache configuration code. In this
file we are going to have a class called <code class="highlighter-rouge">MyCacheSystem</code> and we will
create a <code class="highlighter-rouge">setup</code> function that takes as parameters the CPUs in the
system and the memory controllers.</p>

<p>You can download the complete run script
<a href="_pages/static/scripts/part3/configs/simple_ruby.py">here</a>.</p>

<h2 id="cache-system-configuration">Cache system configuration</h2>

<p>Now, let’s create a file <code class="highlighter-rouge">msi_caches.py</code>. In this file, we will create
four classes: <code class="highlighter-rouge">MyCacheSystem</code> which will inherit from <code class="highlighter-rouge">RubySystem</code>,
<code class="highlighter-rouge">L1Cache</code> and <code class="highlighter-rouge">Directory</code> which will inherit from the SimObjects created
by SLICC from our two state machines, and <code class="highlighter-rouge">MyNetwork</code> which will inherit
from <code class="highlighter-rouge">SimpleNetwork</code>.</p>

<h3 id="l1-cache">L1 Cache</h3>

<p>Let’s start with the <code class="highlighter-rouge">L1Cache</code>. First, we will inherit from
<code class="highlighter-rouge">L1Cache_Controller</code> since we named our L1 cache “L1Cache” in the state
machine file. We also include a special class variable and class method
for tracking the “version number”. For each SLICC state machine, you
have to number them in ascending order from 0. Each machine of the same
type should have a unique version number. This is used to differentiate
the individual machines. (Hopefully, in the future this requirement will
be removed.)</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="nc">L1Cache</span><span class="p">(</span><span class="n">L1Cache_Controller</span><span class="p">):</span>

    <span class="n">_version</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="o">@</span><span class="nb">classmethod</span>
    <span class="k">def</span> <span class="nf">versionCount</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
        <span class="n">cls</span><span class="o">.</span><span class="n">_version</span> <span class="o">+=</span> <span class="mi">1</span> <span class="c1"># Use count for this particular type
</span>        <span class="k">return</span> <span class="n">cls</span><span class="o">.</span><span class="n">_version</span> <span class="o">-</span> <span class="mi">1</span>
</code></pre></div></div>

<p>Next, we implement the constructor for the class.</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">system</span><span class="p">,</span> <span class="n">ruby_system</span><span class="p">,</span> <span class="n">cpu</span><span class="p">):</span>
    <span class="nb">super</span><span class="p">(</span><span class="n">L1Cache</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">()</span>

    <span class="bp">self</span><span class="o">.</span><span class="n">version</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">versionCount</span><span class="p">()</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">cacheMemory</span> <span class="o">=</span> <span class="n">RubyCache</span><span class="p">(</span><span class="n">size</span> <span class="o">=</span> <span class="s">'16kB'</span><span class="p">,</span>
                           <span class="n">assoc</span> <span class="o">=</span> <span class="mi">8</span><span class="p">,</span>
                           <span class="n">start_index_bit</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">getBlockSizeBits</span><span class="p">(</span><span class="n">system</span><span class="p">))</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">clk_domain</span> <span class="o">=</span> <span class="n">cpu</span><span class="o">.</span><span class="n">clk_domain</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">send_evictions</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sendEvicts</span><span class="p">(</span><span class="n">cpu</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">ruby_system</span> <span class="o">=</span> <span class="n">ruby_system</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">connectQueues</span><span class="p">(</span><span class="n">ruby_system</span><span class="p">)</span>
</code></pre></div></div>

<p>We need the CPUs in this function to grab the clock domain and system is
needed for the cache block size. Here, we set all of the parameters that
we named in the state machine file (e.g., <code class="highlighter-rouge">cacheMemory</code>). We will set
<code class="highlighter-rouge">sequencer</code> later. We also hardcode the size an associativity of the
cache. You could add command line parameters for these options, if it is
important to vary them at runtime.</p>

<p>Next, we implement a couple of helper functions. First, we need to
figure out how many bits of the address to use for indexing into the
cache, which is a simple log operation. We also need to decide whether
to send eviction notices to the CPU. Only if we are using the
out-of-order CPU and using x86 or ARM ISA should we forward evictions.</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">def</span> <span class="nf">getBlockSizeBits</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">system</span><span class="p">):</span>
    <span class="n">bits</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">math</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">system</span><span class="o">.</span><span class="n">cache_line_size</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span>
    <span class="k">if</span> <span class="mi">2</span><span class="o">**</span><span class="n">bits</span> <span class="o">!=</span> <span class="n">system</span><span class="o">.</span><span class="n">cache_line_size</span><span class="o">.</span><span class="n">value</span><span class="p">:</span>
        <span class="n">panic</span><span class="p">(</span><span class="s">"Cache line size not a power of 2!"</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">bits</span>

<span class="k">def</span> <span class="nf">sendEvicts</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cpu</span><span class="p">):</span>
    <span class="s">"""True if the CPU model or ISA requires sending evictions from caches
       to the CPU. Two scenarios warrant forwarding evictions to the CPU:
       1. The O3 model must keep the LSQ coherent with the caches
       2. The x86 mwait instruction is built on top of coherence
       3. The local exclusive monitor in ARM systems
    """</span>
    <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">cpu</span><span class="p">)</span> <span class="ow">is</span> <span class="n">DerivO3CPU</span> <span class="ow">or</span> \
       <span class="n">buildEnv</span><span class="p">[</span><span class="s">'TARGET_ISA'</span><span class="p">]</span> <span class="ow">in</span> <span class="p">(</span><span class="s">'x86'</span><span class="p">,</span> <span class="s">'arm'</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">True</span>
    <span class="k">return</span> <span class="bp">False</span>
</code></pre></div></div>

<p>Finally, we need to implement <code class="highlighter-rouge">connectQueues</code> to connect all of the
message buffers to the Ruby network. First, we create a message buffer
for the mandatory queue. Since this is an L1 cache and it will have a
sequencer, we need to instantiate this special message buffer. Next, we
instantiate a message buffer for each buffer in the controller. All of
the “to” buffers we must set the “master” to the network (i.e., the
buffer will send messages into the network), and all of the “from”
buffers we must set the “slave” to the network. These <em>names</em> are the
same as the gem5 ports, but <em>message buffers are not currently
implemented as gem5 ports</em>. In this protocol, we are assuming the
message buffers are ordered for simplicity.</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">def</span> <span class="nf">connectQueues</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ruby_system</span><span class="p">):</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">mandatoryQueue</span> <span class="o">=</span> <span class="n">MessageBuffer</span><span class="p">()</span>

    <span class="bp">self</span><span class="o">.</span><span class="n">requestToDir</span> <span class="o">=</span> <span class="n">MessageBuffer</span><span class="p">(</span><span class="n">ordered</span> <span class="o">=</span> <span class="bp">True</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">requestToDir</span><span class="o">.</span><span class="n">master</span> <span class="o">=</span> <span class="n">ruby_system</span><span class="o">.</span><span class="n">network</span><span class="o">.</span><span class="n">slave</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">responseToDirOrSibling</span> <span class="o">=</span> <span class="n">MessageBuffer</span><span class="p">(</span><span class="n">ordered</span> <span class="o">=</span> <span class="bp">True</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">responseToDirOrSibling</span><span class="o">.</span><span class="n">master</span> <span class="o">=</span> <span class="n">ruby_system</span><span class="o">.</span><span class="n">network</span><span class="o">.</span><span class="n">slave</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">forwardFromDir</span> <span class="o">=</span> <span class="n">MessageBuffer</span><span class="p">(</span><span class="n">ordered</span> <span class="o">=</span> <span class="bp">True</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">forwardFromDir</span><span class="o">.</span><span class="n">slave</span> <span class="o">=</span> <span class="n">ruby_system</span><span class="o">.</span><span class="n">network</span><span class="o">.</span><span class="n">master</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">responseFromDirOrSibling</span> <span class="o">=</span> <span class="n">MessageBuffer</span><span class="p">(</span><span class="n">ordered</span> <span class="o">=</span> <span class="bp">True</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">responseFromDirOrSibling</span><span class="o">.</span><span class="n">slave</span> <span class="o">=</span> <span class="n">ruby_system</span><span class="o">.</span><span class="n">network</span><span class="o">.</span><span class="n">master</span>
</code></pre></div></div>

<h3 id="directory">Directory</h3>

<p>Now, we can similarly implement the directory. There are three
differences from the L1 cache. First, we need to set the address ranges
for the directory. Since each directory corresponds to a particular
memory controller for a subset of the address range (possibly), we need
to make sure the ranges match. The default address ranges for Ruby
controllers is <code class="highlighter-rouge">AllMemory</code>.</p>

<p>Next, we need to set the master port <code class="highlighter-rouge">memory</code>. This is the port that
sends messages when <code class="highlighter-rouge">queueMemoryRead/Write</code> is called in the SLICC code.
We set it the to the memory controller port. Similarly, in
<code class="highlighter-rouge">connectQueues</code> we need to instantiate the special message buffer
<code class="highlighter-rouge">responseFromMemory</code> like the <code class="highlighter-rouge">mandatoryQueue</code> in the L1 cache.</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="nc">DirController</span><span class="p">(</span><span class="n">Directory_Controller</span><span class="p">):</span>

    <span class="n">_version</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="o">@</span><span class="nb">classmethod</span>
    <span class="k">def</span> <span class="nf">versionCount</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
        <span class="n">cls</span><span class="o">.</span><span class="n">_version</span> <span class="o">+=</span> <span class="mi">1</span> <span class="c1"># Use count for this particular type
</span>        <span class="k">return</span> <span class="n">cls</span><span class="o">.</span><span class="n">_version</span> <span class="o">-</span> <span class="mi">1</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ruby_system</span><span class="p">,</span> <span class="n">ranges</span><span class="p">,</span> <span class="n">mem_ctrls</span><span class="p">):</span>
        <span class="s">"""ranges are the memory ranges assigned to this controller.
        """</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">mem_ctrls</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">panic</span><span class="p">(</span><span class="s">"This cache system can only be connected to one mem ctrl"</span><span class="p">)</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">DirController</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">version</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">versionCount</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">addr_ranges</span> <span class="o">=</span> <span class="n">ranges</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ruby_system</span> <span class="o">=</span> <span class="n">ruby_system</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">directory</span> <span class="o">=</span> <span class="n">RubyDirectoryMemory</span><span class="p">()</span>
        <span class="c1"># Connect this directory to the memory side.
</span>        <span class="bp">self</span><span class="o">.</span><span class="n">memory</span> <span class="o">=</span> <span class="n">mem_ctrls</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">port</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">connectQueues</span><span class="p">(</span><span class="n">ruby_system</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">connectQueues</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ruby_system</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">requestFromCache</span> <span class="o">=</span> <span class="n">MessageBuffer</span><span class="p">(</span><span class="n">ordered</span> <span class="o">=</span> <span class="bp">True</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">requestFromCache</span><span class="o">.</span><span class="n">slave</span> <span class="o">=</span> <span class="n">ruby_system</span><span class="o">.</span><span class="n">network</span><span class="o">.</span><span class="n">master</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">responseFromCache</span> <span class="o">=</span> <span class="n">MessageBuffer</span><span class="p">(</span><span class="n">ordered</span> <span class="o">=</span> <span class="bp">True</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">responseFromCache</span><span class="o">.</span><span class="n">slave</span> <span class="o">=</span> <span class="n">ruby_system</span><span class="o">.</span><span class="n">network</span><span class="o">.</span><span class="n">master</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">responseToCache</span> <span class="o">=</span> <span class="n">MessageBuffer</span><span class="p">(</span><span class="n">ordered</span> <span class="o">=</span> <span class="bp">True</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">responseToCache</span><span class="o">.</span><span class="n">master</span> <span class="o">=</span> <span class="n">ruby_system</span><span class="o">.</span><span class="n">network</span><span class="o">.</span><span class="n">slave</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">forwardToCache</span> <span class="o">=</span> <span class="n">MessageBuffer</span><span class="p">(</span><span class="n">ordered</span> <span class="o">=</span> <span class="bp">True</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">forwardToCache</span><span class="o">.</span><span class="n">master</span> <span class="o">=</span> <span class="n">ruby_system</span><span class="o">.</span><span class="n">network</span><span class="o">.</span><span class="n">slave</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">responseFromMemory</span> <span class="o">=</span> <span class="n">MessageBuffer</span><span class="p">()</span>
</code></pre></div></div>

<h3 id="ruby-system">Ruby System</h3>

<p>Now, we can implement the Ruby system object. For this object, the
constructor is simple. It just checks the SCons variable <code class="highlighter-rouge">PROTOCOL</code> to
be sure that we are using the right configuration file for the protocol
that was compiled. We cannot create the controllers in the constructor
because they require a pointer to the this object. If we were to create
them in the constructor, there would be a circular dependence in the
SimObject hierarchy which will cause infinite recursion in when the
system in instantiated with <code class="highlighter-rouge">m5.instantiate</code>.</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="nc">MyCacheSystem</span><span class="p">(</span><span class="n">RubySystem</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">buildEnv</span><span class="p">[</span><span class="s">'PROTOCOL'</span><span class="p">]</span> <span class="o">!=</span> <span class="s">'MSI'</span><span class="p">:</span>
            <span class="n">fatal</span><span class="p">(</span><span class="s">"This system assumes MSI from learning gem5!"</span><span class="p">)</span>

        <span class="nb">super</span><span class="p">(</span><span class="n">MyCacheSystem</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">()</span>
</code></pre></div></div>

<p>Instead of create the controllers in the constructor, we create a new
function to create all of the needed objects: <code class="highlighter-rouge">setup</code>. First, we create
the network. We will look at this object next. With the network, we need
to set the number of virtual networks in the system.</p>

<p>Next, we instantiate all of the controllers. Here, we use a single
global list of the controllers to make it easier to connect them to the
network later. However, for more complicated cache topologies, it can
make sense to use multiple lists of controllers. We create one L1 cache
for each CPU and one directory for the system.</p>

<p>Then, we instantiate all of the sequencers, one for each CPU. Each
sequencer needs a pointer to the instruction and data cache to simulate
the correct latency when initially accessing the cache. In more
complicated systems, you also have to create sequencers for other
objects like DMA controllers.</p>

<p>After creating the sequencers, we set the sequencer variable on each L1
cache controller.</p>

<p>Then, we connect all of the controllers to the network and call the
<code class="highlighter-rouge">setup_buffers</code> function on the network.</p>

<p>We then have to set the “port proxy” for both the Ruby system and the
<code class="highlighter-rouge">system</code> for making functional accesses (e.g., loading the binary in SE
mode).</p>

<p>Finally, we connect all of the CPUs to the ruby system. In this example,
we assume that there are only CPU sequencers so the first CPU is
connected to the first sequencer, and so on. We also have to connect the
TLBs and interrupt ports (if we are using x86).</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">def</span> <span class="nf">setup</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">system</span><span class="p">,</span> <span class="n">cpus</span><span class="p">,</span> <span class="n">mem_ctrls</span><span class="p">):</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">network</span> <span class="o">=</span> <span class="n">MyNetwork</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>

    <span class="bp">self</span><span class="o">.</span><span class="n">number_of_virtual_networks</span> <span class="o">=</span> <span class="mi">3</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">network</span><span class="o">.</span><span class="n">number_of_virtual_networks</span> <span class="o">=</span> <span class="mi">3</span>

    <span class="bp">self</span><span class="o">.</span><span class="n">controllers</span> <span class="o">=</span> \
        <span class="p">[</span><span class="n">L1Cache</span><span class="p">(</span><span class="n">system</span><span class="p">,</span> <span class="bp">self</span><span class="p">,</span> <span class="n">cpu</span><span class="p">)</span> <span class="k">for</span> <span class="n">cpu</span> <span class="ow">in</span> <span class="n">cpus</span><span class="p">]</span> <span class="o">+</span> \
        <span class="p">[</span><span class="n">DirController</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">system</span><span class="o">.</span><span class="n">mem_ranges</span><span class="p">,</span> <span class="n">mem_ctrls</span><span class="p">)]</span>

    <span class="bp">self</span><span class="o">.</span><span class="n">sequencers</span> <span class="o">=</span> <span class="p">[</span><span class="n">RubySequencer</span><span class="p">(</span><span class="n">version</span> <span class="o">=</span> <span class="n">i</span><span class="p">,</span>
                            <span class="c1"># I/D cache is combined and grab from ctrl
</span>                            <span class="n">icache</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">controllers</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">cacheMemory</span><span class="p">,</span>
                            <span class="n">dcache</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">controllers</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">cacheMemory</span><span class="p">,</span>
                            <span class="n">clk_domain</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">controllers</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">clk_domain</span><span class="p">,</span>
                            <span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">cpus</span><span class="p">))]</span>

    <span class="k">for</span> <span class="n">i</span><span class="p">,</span><span class="n">c</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">controllers</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sequencers</span><span class="p">)]):</span>
        <span class="n">c</span><span class="o">.</span><span class="n">sequencer</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sequencers</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>

    <span class="bp">self</span><span class="o">.</span><span class="n">num_of_sequencers</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sequencers</span><span class="p">)</span>

    <span class="bp">self</span><span class="o">.</span><span class="n">network</span><span class="o">.</span><span class="n">connectControllers</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">controllers</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">network</span><span class="o">.</span><span class="n">setup_buffers</span><span class="p">()</span>

    <span class="bp">self</span><span class="o">.</span><span class="n">sys_port_proxy</span> <span class="o">=</span> <span class="n">RubyPortProxy</span><span class="p">()</span>
    <span class="n">system</span><span class="o">.</span><span class="n">system_port</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sys_port_proxy</span><span class="o">.</span><span class="n">slave</span>

    <span class="k">for</span> <span class="n">i</span><span class="p">,</span><span class="n">cpu</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">cpus</span><span class="p">):</span>
        <span class="n">cpu</span><span class="o">.</span><span class="n">icache_port</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sequencers</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">slave</span>
        <span class="n">cpu</span><span class="o">.</span><span class="n">dcache_port</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sequencers</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">slave</span>
        <span class="n">isa</span> <span class="o">=</span> <span class="n">buildEnv</span><span class="p">[</span><span class="s">'TARGET_ISA'</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">isa</span> <span class="o">==</span> <span class="s">'x86'</span><span class="p">:</span>
            <span class="n">cpu</span><span class="o">.</span><span class="n">interrupts</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">pio</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sequencers</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">master</span>
            <span class="n">cpu</span><span class="o">.</span><span class="n">interrupts</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">int_master</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sequencers</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">slave</span>
            <span class="n">cpu</span><span class="o">.</span><span class="n">interrupts</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">int_slave</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sequencers</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">master</span>
        <span class="k">if</span> <span class="n">isa</span> <span class="o">==</span> <span class="s">'x86'</span> <span class="ow">or</span> <span class="n">isa</span> <span class="o">==</span> <span class="s">'arm'</span><span class="p">:</span>
            <span class="n">cpu</span><span class="o">.</span><span class="n">itb</span><span class="o">.</span><span class="n">walker</span><span class="o">.</span><span class="n">port</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sequencers</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">slave</span>
            <span class="n">cpu</span><span class="o">.</span><span class="n">dtb</span><span class="o">.</span><span class="n">walker</span><span class="o">.</span><span class="n">port</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">sequencers</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">slave</span>
</code></pre></div></div>

<h3 id="network">Network</h3>

<p>Finally, the last object we have to implement is the network. The
constructor is simple, but we need to declare an empty list for the list
of network interfaces (<code class="highlighter-rouge">netifs</code>).</p>

<p>Most of the code is in <code class="highlighter-rouge">connectControllers</code>. This function implements a
<em>very simple, unrealistic</em> point-to-point network. In other words, every
controller has a direct link to every other controller.</p>

<p>The Ruby network is made of three parts: routers that route data from
one router to another or to external controllers, external links that
link a controller to a router, and internal links that link two routers
together. First, we create a router for each controller. Then, we create
an external link from that router to the controller. Finally, we add all
of the “internal” links. Each router is connected to all other routers
to make the point-to-point network.</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="nc">MyNetwork</span><span class="p">(</span><span class="n">SimpleNetwork</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ruby_system</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">MyNetwork</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">netifs</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ruby_system</span> <span class="o">=</span> <span class="n">ruby_system</span>

    <span class="k">def</span> <span class="nf">connectControllers</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">controllers</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">routers</span> <span class="o">=</span> <span class="p">[</span><span class="n">Switch</span><span class="p">(</span><span class="n">router_id</span> <span class="o">=</span> <span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">controllers</span><span class="p">))]</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">ext_links</span> <span class="o">=</span> <span class="p">[</span><span class="n">SimpleExtLink</span><span class="p">(</span><span class="n">link_id</span><span class="o">=</span><span class="n">i</span><span class="p">,</span> <span class="n">ext_node</span><span class="o">=</span><span class="n">c</span><span class="p">,</span>
                                        <span class="n">int_node</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">routers</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
                          <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">c</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">controllers</span><span class="p">)]</span>

        <span class="n">link_count</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">int_links</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">ri</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">routers</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">rj</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">routers</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">ri</span> <span class="o">==</span> <span class="n">rj</span><span class="p">:</span> <span class="k">continue</span> <span class="c1"># Don't connect a router to itself!
</span>                <span class="n">link_count</span> <span class="o">+=</span> <span class="mi">1</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">int_links</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">SimpleIntLink</span><span class="p">(</span><span class="n">link_id</span> <span class="o">=</span> <span class="n">link_count</span><span class="p">,</span>
                                                    <span class="n">src_node</span> <span class="o">=</span> <span class="n">ri</span><span class="p">,</span>
                                                    <span class="n">dst_node</span> <span class="o">=</span> <span class="n">rj</span><span class="p">))</span>
</code></pre></div></div>

<p>You can download the complete <code class="highlighter-rouge">msi_caches.py</code> file
<a href="/_pages/static/scripts/part3/configs/msi_caches.py">here</a>.</p>

  <br>

  <!-- RETRIVE PREVIOUS PAGE LINK -->
  
    
  
    
  
    
  
    
  

  <!-- RETRIEVE NEXT PAGE LINK -->
  
    
  
    
  
    
  
    
  


  <div class="navbuttons">
    
      <a href=""><button type="button" class="btn btn-outline-primary">PREVIOUS</button></a>
    

    
      <a href=""><button type="button" class="btn btn-outline-primary">NEXT</button></a>
    
  </div>
</div>

	</main>
	<footer class="page-footer">
	<div class="container">
		<div class="row">

			<div class="col-12 col-sm-4">
				<p>gem5</p>
				<p><a href="/about">About</a></p>
				<p><a href="/publications">Publications</a></p>
				<p><a href="/contributing">Contributing</a></p>
				<p><a href="/governance">Governance</a></p>
			<br></div>

			<div class="col-12 col-sm-4">
				<p>Docs</p>
				<p><a href="/documentation">Documentation</a></p>
				<p><a href="http://gem5.org/Documentation">Old Documentation</a></p>
				<p><a href="https://gem5.googlesource.com/public/gem5">Source</a></p>
			<br></div>

			<div class="col-12 col-sm-4">
				<p>Help</p>
				<p><a href="/search">Search</a></p>
				<p><a href="/mailing_lists">Mailing Lists</a></p>
				<p><a href="https://gem5.googlesource.com/public/gem5-website/+/refs/heads/master/README.md">Website Source</a></p>
			<br></div>

		</div>
	</div>
</footer>


	<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
	<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
	<script src="https://unpkg.com/commentbox.io/dist/commentBox.min.js"></script>

	<script>
	  // When the user scrolls down 20px from the top of the document, show the button
	  window.onscroll = function() {scrollFunction()};

	  function scrollFunction() {
	      if (document.body.scrollTop > 100 || document.documentElement.scrollTop > 20) {
	          document.getElementById("myBtn").style.display = "block";
	      } else {
	          document.getElementById("myBtn").style.display = "none";
	      }
	  }

	  // When the user clicks on the button, scroll to the top of the document
	  function topFunction() {
	      document.body.scrollTop = 0;
	      document.documentElement.scrollTop = 0;
	  }

		import commentBox from 'commentbox.io';
		// or
		const commentBox = require('commentbox.io');
		// or if using the CDN, it will be available as a global "commentBox" variable.

		commentBox('my-project-id');

	</script>

</body>


</html>
